<?php

namespace Modules\Shop\Http\Controllers;

use foo\bar;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Shop\Entities\Category;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {


        $gory = Category::paginate(5);
        $cate = Category::get();
//        dump($cate);
//        return 143;
//        dd($cate->toArray());
//        这句话的意思是加载shop模块下的resources\shop\index.blade.php模板
        return view('shop::shop.index', compact('cate', 'gory'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('shop::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {


//        echo  111;
//        dd($request->toArray());
//        可以进来，下一步，就是写数据
        Category::create($request->all());
        session()->flash('success', '商品添加成功');
        return back();
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('shop::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('shop::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request, $id)
    {

//        dd(123);
//        dd($request->toArray());
//        dd($id);
        $shop = Category::find($id);
//        dd($shop->toArray());
        $shop->shopname = $request->input('shopname');
        $shop->pid = $request->input('pid');
        $shop->save();
        session()->flash('success', '编辑成功，欢迎下次编辑');
        return redirect()->route('category.index');
//        编辑写完，写删除
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
//        可以进来
        $category  =  Category::find($id);
////       dd($category['pid']);
//        $ziji = $category['pid'];
//        if (!$ziji){
//            session()->flash('danger','不能删除顶级菜单！！！');
//            return back();
//        }
//        dd(111);
//        判断它里面有没有子集，有子集的话机先判断确定要删吗
//        dd(Category::hasChildCategory($category));
//        根据结果，返回的是真假值，根据真假进行判断
        if (Category::hasChildCategory($category)){
//            dd(123);
            session()->flash('danger','请先删除子类，再删除父类');
            return back();
        }
        Category::destroy($id);
        session()->flash('success','恭喜删除成功');
        return redirect()->route('category.index');
//        先弄自动化构建吧
//        问题解决，下一步解决下拉列表默认不选中的效果
//        hasChildCategory找的是他里面再有没有值
    }
}